package renguanyu.app.auth4fx.controller.user;

import javafx.concurrent.Task;
import renguanyu.app.auth4fx.model.Page;
import renguanyu.app.auth4fx.model.user.UserDto;
import renguanyu.app.auth4fx.model.user.UserVo;
import renguanyu.app.auth4fx.service.user.UserService;
import renguanyu.app.auth4fx.util.PageUtil;
import renguanyu.app.auth4fx.view.user.UserListView;

import java.util.List;
import java.util.Map;

public class UserListQueryController extends Task<Void> {

    private UserListView userListView;
    public UserListQueryController(UserListView userListView){
        this.userListView = userListView;
    }

    public void startAsyncQuery() {
        System.out.println("UserListQuery.execute");
        Thread thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    Page page;
    @Override
    protected Void call() {
        System.out.println("UserListQuery.call");
        UserDto userDto = buildUserDto();
        page = UserService.getUserPage(userDto);
        return null;
    }

    private UserDto buildUserDto() {
        // 接收参数
        int pageSize = userListView.pageSizeProperty.get();
        int currentPageIndex = userListView.currentPageIndexProperty.get();
        String searchKey = userListView.searchFieldKey.getValue();
        String searchValue = userListView.searchFieldValue.getValue();
        // 格式化参数
        String name = "";
        String password = "";
        if ("用户名".equals(searchKey) && searchValue!=null && !searchValue.isBlank()) {
            name = "%"+searchValue+"%";
        }
        if ("密码".equals(searchKey) && searchValue!=null && !searchValue.isBlank()) {
            password = "%"+searchValue+"%";
        }
        // 封装DTO
        UserDto userDto = new UserDto();
        userDto.setPageSize(pageSize);
        userDto.setCurrentPageIndex(currentPageIndex);
        userDto.setName(name);
        userDto.setPassword(password);
        return userDto;
    }


    @Override
    protected void running() {
        System.out.println("UserListQuery.running");
        userListView.dataPaneStatusLoading();
        userListView.dataPaneObservableList.clear();
        userListView.selectAllBooleanProperty.set(false);
    }

    @Override
    protected void succeeded() {
        System.out.println("UserListQuery.succeeded");
        // 无数据
        if(page.getPageData().isEmpty()){
            userListView.dataPaneStatusNoData();
            userListView.pageCountProperty.set(1);
            userListView.maxPageIndicatorCount.set(1);
            return;
        }
        // 有数据
        Integer pageCount = page.getPageCount();
        userListView.pageCountProperty.set(pageCount);
        userListView.maxPageIndicatorCount.set(PageUtil.getMaxPageIndicatorCount(pageCount));
        userListView.dataPaneObservableList.setAll(convertToUserVoList(page.getPageData()));
    }

    private List<UserVo> convertToUserVoList(List<Map<String,Object>> pageData) {
        return pageData.stream().map(item -> {
            UserVo userVo = new UserVo();
            userVo.setId(item.get("user_id").toString());
            userVo.setName(item.get("user_name").toString());
            userVo.setPassword(item.get("password").toString());
            userVo.setRoles(item.get("role_names")==null?"":item.get("role_names").toString());
            return userVo;
        }).toList();
    }


    @Override
    protected void failed() {
        System.out.println("UserListQuery.failed");
        userListView.dataPaneStatusError();
        userListView.pageCountProperty.set(1);
        userListView.maxPageIndicatorCount.set(1);
    }

}
